<?php
namespace Admin\Model;
use Think\Model;
class ItemModel extends Model
{
    /**
     * @return array
     *  道具添加
     */
    public function addProp()
    {
        $info = I('post.');
        unset($info['action']);
        $makeID = new \Org\Twitter\IdWork();
        $info['id'] = $makeID->nextId();
        $info['icon'] = $this->uploadImg();
        $info['createtime'] = time();
        $info['updatetime'] = time();
        $info['admin'] = $_SESSION['admin'];
        $option = $this->add($info);
        $data = [
            'success'=>1,
            'msg'=>'信息添加成功',
            'id'=>Null
        ];
        if(!$option){
            $data = [
                'success'=>0,
                'msg'=>'信息添加失败',
                'id'=>Null
            ];
        }
        return $data;

    }

    /**
     * @return array
     * 道具信息修改
     */

    public function updateProp(){
        $info = I('post.');
        unset($info['action']);
        unset($info['itemid']);
        if(!empty($_FILES['icon']['name'])){
            $info['icon'] = $this->uploadImg();
        }
        $info['updatetime'] =time();
        $option = $this->where(['itemid'=>$_POST['itemid']])->save($info);
        $data = [
                'success'=>1,
                'msg'=>'信息修改成功',
                'id'=>$_POST['itemid']
            ];
        if(!$option){
            $data = [
                'success'=>0,
                'msg'=>'信息修改失败',
                'id'=>$_POST['itemid']
            ];
        }
        return $data;
    }
    /**
     * @return string
     * 道具图片上传
     */
    public function  uploadImg(){
        $upload = new \Think\Upload();
        $upload->maxSize   =    3145728 ;
        $upload->exts      =    array('jpg', 'gif', 'png', 'jpeg');
        $upload->rootPath  =    './Public/Img/';
        $upload->saveName  =    array('uniqid','');
        $upload->autoSub   =    true;
        $upload->subName   =    array('date','Ymd');

        $uploadinfo   =   $upload->uploadOne($_FILES['icon']);
        if(!$uploadinfo) {
            return $upload->getError();
        }else{
            $imgPath = './Public/Img/'.$uploadinfo['savepath'].$uploadinfo['savename'];
        }
        return $imgPath;
    }
}

